import {MainData, PK, pkType, StatusData} from "../../coreModule/data/BaseData";
import {DataOccasion, field, occasion} from "../../coreModule/data/DataLoader";
import {Bill} from "./Bill";
import {TradeStatus} from "./Trade";
import {DateUtils} from "../../../utils/DateUtils";

export enum ContractStatus {
	Opened = -2, Checking, Active,
	Overdue, Broken,
	Finished, Cancelled
}

export class Contract extends StatusData<ContractStatus> {

	@field(pkType())
	public userId: PK; // 用户
	@field(pkType())
	public taskId: PK; // 任务

	@field(Number)
	public price: number;
	@field(String)
	public description: string;

	@field(String)
	public email: string;
	@field(String)
	public name: string;
	@field(String)
	public cardNo: string;
	@field(String)
	public picture: string;

	@field(Number)
	public createdAt: number;
	@field(Number)
	public startTime: number; // 合同开始时间
	@field(Number)
	public endTime: number; // 合同结束时间

	@field
	public status: ContractStatus = ContractStatus.Opened;

	// region 额外信息

	@field(String)
	@occasion(DataOccasion.Extra)
	priceStr: string

	@field(String)
	@occasion(DataOccasion.Extra)
	statusStr: string

	@field(String)
	@occasion(DataOccasion.Extra)
	createTimeStr: string
	@field(String)
	@occasion(DataOccasion.Extra)
	startTimeStr: string
	@field(String)
	@occasion(DataOccasion.Extra)
	endTimeStr: string

	public refresh() {
		this.priceStr = (this.price / 100).toFixed(2);
		this.statusStr = this.getStatusStr();

		this.createTimeStr = DateUtils.time2Str(this.createdAt);
		this.startTimeStr = DateUtils.time2Str(this.startTime);
		this.endTimeStr = DateUtils.time2Str(this.endTime);
	}
	private getStatusStr() {
		switch (this.status) {
			case ContractStatus.Opened: return "已发起";
			case ContractStatus.Checking: return "审核中";
			case ContractStatus.Active: return "已启用";
			case ContractStatus.Finished: return "已完成";
			case ContractStatus.Overdue: return "已逾期";
			case ContractStatus.Broken: return "已违约";
			case ContractStatus.Cancelled: return "已取消";
			default: return "异常";
		}
	}

	// endregion

	// region 条件判定

	/**
	 * 是否开始
	 */
	public isStarted() { return Date.now() >= this.startTime; }

	/**
	 * 是否结束
	 */
	public isEnd() { return Date.now() >= this.endTime; }

	// endregion

}
